package cn.edu.xjtu.nbrash.xbarsuite.zxingdemo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.telephony.PhoneNumberUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.zxing.WriterException;
import com.google.zxing.client.android.Contents;
import com.google.zxing.client.android.Intents;
//import com.google.zxing.client.android.encode.ContactEncoder;
//import com.google.zxing.client.android.encode.MECARDContactEncoder;
//import com.google.zxing.client.android.encode.QRCodeEncoder;
//import com.google.zxing.client.android.encode.VCardContactEncoder;

import com.google.zxing.client.android.encode.QRCodeEncoder;

import cn.edu.xjtu.nbrash.xbarsuite.R;

public class RashDisplayActivity extends Activity {

	private ImageView imgView;
	private Bitmap bitmap;
	// final String FILE_BITMAP = "bitmap";

	String fileName = "fn";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rash_display);

		imgView = (ImageView) findViewById(R.id.imgQR);

	}

	@Override
	protected void onResume() {
		super.onResume();

		// 以下是一段获取屏幕尺寸的标准代码
		WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		Point displaySize = new Point();
		display.getSize(displaySize);
		int width = displaySize.x;
		int height = displaySize.y;
		// 这里所做的工作是为了得到，较小的一个维度，以获取二维码在屏幕上较好的观感
		int smallerDimension = width < height ? width : height;

		// 使用QRCodeEncoder时，需要传入一个Intent作为各种参数的载体，本例以本身Activity的Intent为例
		Intent intent = getIntent();

		try {
			QRCodeEncoder qrCoderEncoder = new QRCodeEncoder(this, intent,
					smallerDimension, false);
			bitmap = qrCoderEncoder.encodeAsBitmap();
			if (bitmap != null) {
				imgView.setImageBitmap(bitmap);
			} else {
				// TODO : do something to deal with when there is no image
			}
		} catch (WriterException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 保存生成的图片
	 * 
	 * @param v
	 * @throws IOException
	 */

	public void saveBitmap(View v) throws IOException {

		Intent intent = getIntent();
		String type = intent.getStringExtra(Intents.Encode.TYPE);

		switch (type) {
		case Contents.Type.TEXT: {
			fileName = "文本_" + intent.getStringExtra(Intents.Encode.DATA);
			if (fileName.length() > 9)
				fileName = fileName.substring(0, 9) + "...";

			break;
		}
		case Contents.Type.EMAIL: {
			fileName = "邮件_" + intent.getStringExtra(Intents.Encode.DATA);
			// int index = fileName.indexOf("?");
			// fileName = fileName.substring(0,index);
			int indexOfQuestion = fileName.indexOf("?");
			String part1 = fileName.substring(0, indexOfQuestion);

			int indexOfAnd = fileName.indexOf("&");
			int indexOfEqual = fileName.indexOf("=");
			String part2 = fileName.substring(indexOfEqual + 1, indexOfAnd);
			fileName = part1 + "_" + part2;
			break;
		}
		case Contents.Type.PHONE: {
			fileName = "电话_" + intent.getStringExtra(Intents.Encode.DATA);

			break;
		}
		case Contents.Type.SMS: {
			fileName = "短信_" + intent.getStringExtra(Intents.Encode.DATA);
			int indexOfQuestion = fileName.indexOf("?");
			String part1 = fileName.substring(0, indexOfQuestion);
			int indexOfEqual = fileName.indexOf("=");

			String part2 = fileName.substring(indexOfEqual + 1,
					fileName.length());
			if (part2.length() > 5)
				part2 = part2.substring(0, 5) + "...";
			fileName = part1 + "_" + part2;

			break;
		}
		case Contents.Type.CONTACT: {

			Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA);
			if (bundle != null) {

				String name = bundle
						.getString(ContactsContract.Intents.Insert.NAME);
				String organization = bundle
						.getString(ContactsContract.Intents.Insert.COMPANY);
				String address = bundle
						.getString(ContactsContract.Intents.Insert.POSTAL);
				fileName = "名片_" + name;
			}
			break;
		}

		}

		if (hasSD()) {
			String SDPath = getSDPath();

			File dir = new File(SDPath + "/RashCode");
			if (!dir.exists()) {
				dir.mkdirs();
			}
			try {
				

				File f = new File(SDPath + "/RashCode", fileName + ".png");

				FileOutputStream fos = new FileOutputStream(f);

				bitmap.compress(Bitmap.CompressFormat.PNG, 75, fos);
				Toast.makeText(this, "已保存", Toast.LENGTH_LONG).show();

				fos.flush();
				fos.close();

			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	public String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
		}
		// else{
		// Toast.makeText(this, "SD卡不存在！无法保存", Toast.LENGTH_LONG).show();
		//
		// }
		return sdDir.toString();

	}

	public boolean hasSD() {
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (!sdCardExist) {
			Toast.makeText(this, "SD卡不存在！无法保存", Toast.LENGTH_LONG).show();

		}
		return sdCardExist;

	}

}
